<template>
  <el-dialog
    class="basic-dialog"
    :model-value="visible"
    title="提示"
    :width="400"
    :top="top"
    :close-on-click-modal="!loading"
    :close-on-press-escape="!loading"
    show-close
    @closed="handleClosed"
  >
    <div class="w-full pb-[36px]">
      <span v-if="content"> {{ content }}</span>
      <slot v-else></slot>
    </div>
    <template #footer>
      <section v-if="justTips">
        <el-button type="primary" @click="handleClosed"> 我知道了 </el-button>
      </section>
      <section v-else>
        <el-button
          :loading="loading"
          type="primary"
          class="px-6"
          @click="handleSubmit"
        >
          确定
        </el-button>
        <el-button :loading="loading" @click="emit('update:visible', false)"
          >取消</el-button
        >
      </section>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { has } from 'lodash-es'

import { useAppStore } from '@/stores/modules/app'
interface IProps {
  top?: string
  visible: boolean
  idList?: string[]
  record?: any
  content?: string
  command?: string
  justTips?: boolean
}
interface IEmits {
  // 更新 显示状态
  (event: 'update:visible', value: boolean): void
  // 通知上层更新
  (event: 'reload'): void
}

type Command =
  | 'logout'
  | 'resetPassword'
  | 'delete'
  | 'toggleStatus'
  | 'batchDeleteUserGroup'

const emit = defineEmits<IEmits>()
const props = withDefaults(defineProps<IProps>(), {
  justTips: false,
  command: '',
  top: '15vh'
})
const loading = ref(false)
const appStore = useAppStore()
/**
 * 保存方法
 */
const handleSubmit = async () => {
  loading.value = true
  const markCommandMap: Record<Command, () => void> = {
    logout: function (): void {
      appStore.signOutAction().finally(() => {
        loading.value = false
        emit('update:visible', false)
      })
    },
    resetPassword: () => {
      resetAdminPasswordApi({ id: props.record?.id as string })
        .then(() => {
          ToastUtils.success('重置成功')
          emit('reload')
          emit('update:visible', false)
        })
        .finally(() => {
          loading.value = false
        })
    },
    delete: () => {
      deleteAdminApi({ id: props.record?.id as string })
        .then(() => {
          ToastUtils.success('删除成功')
          emit('reload')
          emit('update:visible', false)
        })
        .finally(() => {
          loading.value = false
        })
    },
    toggleStatus: () => {
      const data = {
        id: props.record?.id as string,
        status: props.record?.status === 1 ? 2 : 1,
        remark: props.record?.remark as string
      }
      putAdminApi(data)
        .then(() => {
          if (data.status === 1) {
            ToastUtils.success('启用成功')
          } else {
            ToastUtils.success('禁用成功')
          }
          emit('update:visible', false)
          emit('reload')
        })
        .finally(() => {
          loading.value = false
        })
    },
    batchDeleteUserGroup: () => {
      deleteUserGroupBatchApi({ ids: props.record })
        .then(() => {
          ToastUtils.success('删除成功')
          emit('reload')
          emit('update:visible', false)
        })
        .finally(() => {
          loading.value = false
        })
    }
  }
  if (has(markCommandMap, props.command)) {
    markCommandMap[props.command]()
  }
}
/**
 * @description dialog 关闭前事件
 */
const handleClosed = () => {
  emit('update:visible', false)
}
</script>

<style scoped></style>
